package claybird.dom;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import org.springframework.beans.factory.annotation.Autowired;

import claybird.dao.ResultDao;

/*
 *  Represent a team of shooter
 */
@Entity
public class Team  extends BaseEntity{
	@ManyToMany
	private List<Person> listPerson = new ArrayList<Person>();
	@ManyToOne
	private ShootDay shootDay;
	private float averageHandicap =0;
	private float totalResult = 0;
	private float averageHandicapWithGuest =0;
	private float totalResultWithGuest = 0;
	private Short ranking;
	private Short rankingWithGuest;
	private String name;
	
	public Team(){
		
	}
	public Team(String name, ShootDay shootDay){
		this.shootDay = shootDay;
		this.name = name;
	}
	public List<Person> getListPerson() {
		return listPerson;
	}

	public void setListPerson(List<Person> listPersonne) {
		this.listPerson = listPersonne;
	}
	public void addToList(Person personne){
		this.listPerson.add(personne);
	}
	public void removeFromList(Person personne){
		this.listPerson.remove(personne);
	}
	
	public Short getRanking() {
		return ranking;
	}
	public void setRanking(Short classement) {
		this.ranking = classement;
	}
	public Short getRankingWithGuest() {
		return rankingWithGuest;
	}
	public void setRankingWithGuest(Short rankingWithGuest) {
		this.rankingWithGuest = rankingWithGuest;
	}

	public ShootDay getShootDay() {
		return shootDay;
	}

	public void setShootDay(ShootDay tir) {
		this.shootDay = tir;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	private float getAverageHandicapOfTeam(){
		float total =0;
		for(Person shooter : this.getListWithoutGuess()){
			if (!shooter.isGuest())
				total = total+shooter.getLastHandicap().getCurrentHandicap();
		}
		this.averageHandicap=total/ this.getListWithoutGuess().size();
		return total/ this.getListWithoutGuess().size();
	}
	private float getTotalPointOfTeam(){
		float total =0;
		List<Result> results = new ArrayList<Result>();
		for(Person shooter : this.getListWithoutGuess()){
			if (!shooter.isGuest())
				results.add(shooter.getResultFor(this.shootDay));
		}
		for(Result res : results){
			total = total+res.getRealValue();
		}
		if (results.size()==this.getListWithoutGuess().size()){
			this.totalResult = total;
			return results.size();
		}else{
			throw new Error("nombre de resultats non egal au nombre de personne dans la team");
		}
	}
	private float getAverageHandicapOfTeamWithGuest(){
		float total =0;
		for(Person shooter : this.listPerson){
			if (!shooter.isGuest())
				total = total+shooter.getLastHandicap().getCurrentHandicap();
		}
		this.averageHandicapWithGuest=total/ this.listPerson.size();
		return total/ this.listPerson.size();
	}
	private float getTotalPointOfTeamWithGuest(){
		float total =0;
		List<Result> results = new ArrayList<Result>();
		for(Person shooter : this.listPerson){
			
				results.add(shooter.getResultFor(this.shootDay));
		}
		for(Result res : results){
			total = total+res.getRealValue();
		}
		if (results.size()==this.listPerson.size()){
			this.totalResultWithGuest = total;
			return results.size();
		}else{
			throw new Error("nombre de resultats non egal au nombre de personne dans la team");
		}
	}
	public float getAverageHandicap() {
		if ( this.averageHandicap==0){
			return this.getAverageHandicapOfTeam();
		}
		return averageHandicap;
	}
	public void setAverageHandicap(float averageHandicap) {
		this.averageHandicap = averageHandicap;
	}
	public float getTotalResult() {
		if (this.totalResult==0){
			return this.getTotalPointOfTeam();
		}
		return totalResult;
	}
	public void setTotalResult(float totalResult) {
		this.totalResult = totalResult;
	}
	public float getAverageTotal() {
		if (this.totalResult==0){
			this.getTotalPointOfTeam();
		}
		
		return this.totalResult/this.listPerson.size();
	}
	public float getTeamValue(float bestTeamStrength) {
		// Pour compenser les differences de plateaux, 
		//ajout de la moiti2 de la difference de handicap pr rapport au meilleur
		return this.getAverageTotal()*((bestTeamStrength / this.getAverageHandicap()));
	}
	public float getAverageHandicapWithGuest() {
		if (this.averageHandicapWithGuest==0){
			return this.getAverageHandicapOfTeamWithGuest();
		}
		return averageHandicapWithGuest;
	}
	public void setAverageHandicapWithGuest(float averageHandicapWithGuest) {
		this.averageHandicapWithGuest = averageHandicapWithGuest;
	}
	public float getTotalResultWithGuest() {
		if (this.totalResultWithGuest==0){
			return this.getTotalPointOfTeamWithGuest();
		}
		return totalResultWithGuest;
	}
	public void setTotalResultWithGuest(float totalResultWithGuest) {
		this.totalResultWithGuest = totalResultWithGuest;
	}
	public float getAverageTotalWithGuest() {
		if (this.totalResultWithGuest==0){
			this.getTotalPointOfTeamWithGuest();
		}
		
		return this.totalResultWithGuest/this.listPerson.size();
	}
	public float getTeamValueWithGuest(float bestTeamStrength) {
		// Pour compenser les differences de plateaux, 
		//ajout de la moiti2 de la difference de handicap pr rapport au meilleur
		return this.getAverageTotalWithGuest()*((bestTeamStrength / this.getAverageHandicapWithGuest()));
	}
	public List<Person> getListWithoutGuess(){
		List<Person> list = new ArrayList<Person>();
		for (Person pers : this.listPerson){
			if (!pers.isGuest()){
				list.add(pers);
			}
		}
		return list;
	}
}
